package MULE.Model.Panel;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import MULE.Model.Logic.PlayerConfigLogic;

/**
 * Panel designed for configuring MULE game players. Allows control over player
 * name, race, and color.
 * 
 * @author jstep
 */
public class PlayerConfigPanel extends JPanel {

	private JLabel panelTitle;
	private JTextField nameField;
	private JComboBox raceList, colorList;
	private JButton nextButton;

	/**
	 * Creates a new player configuration panel. Displays given player options.
	 * 
	 * @param races
	 *            List of available player races.
	 * @param colors
	 *            List of available player colors.
	 */
	public PlayerConfigPanel(String[] races, String[] colors) {
		this.setLayout(new GridLayout(0, 1));
		// Create and add panel title.
		this.panelTitle = new JLabel("Player 1 Configuration");
		panelTitle.setFont(new Font("Verdana", Font.BOLD, 20));
		// panelTitle.setBounds(this.xOffset, this.yOffset, 200, 20);
		this.add(panelTitle);

		// Create name label and text field inside of separate panel.
		JPanel namePanel = new JPanel();
		namePanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		// namePanel.setBackground(Color.BLUE); // debug layout
		JLabel nameLabel = new JLabel("NAME");
		namePanel.add(nameLabel);
		this.nameField = new JTextField();
		nameField.setPreferredSize(new Dimension(200, 30));
		namePanel.add(nameField);
		this.add(namePanel);

		// Create race selection drop-down.
		JPanel racePanel = new JPanel();
		racePanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		JLabel raceLabel = new JLabel("RACE");
		racePanel.add(raceLabel);
		this.raceList = new JComboBox(races);
		racePanel.add(raceList);
		this.add(racePanel);

		// Create color selection drop-down.
		JPanel colorPanel = new JPanel();
		colorPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		JLabel colorLabel = new JLabel("COLOR");
		colorPanel.add(colorLabel);
		this.colorList = new JComboBox(colors);
		colorPanel.add(colorList);
		this.add(colorPanel);

		// Create and add "Next" button.
		this.nextButton = new JButton("Next");
		String nextCommand = PlayerConfigLogic.ActionCommand.NEXT.toString();
		nextButton.setActionCommand(nextCommand);
		this.add(nextButton);
	}

	/**
	 * Removes the given color from the panel's list of available colors.
	 * 
	 * @param colorName
	 *            The color to remove from the list.
	 */
	public void removeColorFromList(String colorName) {
		// Make argument ignore case.
		colorName = colorName.toLowerCase();
		// Look through color selection items.
		DefaultComboBoxModel colorModel = (DefaultComboBoxModel) colorList
				.getModel();
		for (int i = 0; i < colorModel.getSize(); ++i) {
			String item = colorModel.getElementAt(i).toString().toLowerCase();
			if (item.equals(colorName)) {
				colorModel.removeElementAt(i);
				return;
			}
		}
	}

	/**
	 * Returns the name entered in the panel's name field.
	 * 
	 * @return The player's name as typed by the user.
	 */
	public String getPlayerName() {
		return nameField.getText();
	}

	/**
	 * Returns the race selected in the panel's race field.
	 * 
	 * @return The player's race as selected by the user.
	 */
	public String getPlayerRace() {
		return raceList.getSelectedItem().toString();
	}

	/**
	 * Returns the color selected in the panel's color field.
	 * 
	 * @return The player's color as selected by the user.
	 */
	public String getPlayerColor() {
		return colorList.getSelectedItem().toString();
	}

	/**
	 * Resets this panel for the given player number. Clears all selections and
	 * changes panel title.
	 * 
	 * @param playerNum
	 *            The number of the next player to be configured.
	 */
	public void resetPlayerConfig(int playerNum) {
		// Reset panel title with new player number.
		panelTitle.setText("Player " + playerNum + " Configuration");
		// Clear selections for previous player.
		nameField.setText("");
		raceList.setSelectedIndex(0);
		colorList.setSelectedIndex(0);
		// Give name field focus.
		nameField.requestFocusInWindow();
	}

	/**
	 * Adds an action listener to the panel's next button.
	 * 
	 * @param listener
	 *            The listener that should listen to the next button.
	 */
	public void addNextButtonListener(ActionListener listener) {
		nextButton.addActionListener(listener);
	}

	/**
	 * Listens for this panel to be added. Overridden to request window focus in
	 * the text field as soon as the view is shown.
	 */
	public void addNotify() {
		super.addNotify();
		nameField.requestFocusInWindow();
	}
}
